#include "canvas.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTranslator>
#include <QApplication>
#include <QScreen>
#include <QGuiApplication>
#include <QPainter>
#include <QDateTime>
#include <QFileDialog>
#include <QFileInfo>
#include <QColorDialog>
#include <QDesktopWidget>

#include "operateset.hpp"
#include "const.h"

Canvas::Canvas(QWidget *parent) : QWidget(parent)
{
    this->mOkFlag=1;

    const QRect screenGeometry = QApplication::desktop()->screenGeometry(this);
    this->mScreen_width=screenGeometry.width();
    this->mScreen_height=screenGeometry.height();

    this->raise();                      //软置顶
    this->showFullScreen();             //画布全屏显示

    this->mpClipboard = QApplication::clipboard();   //获取系统剪贴板指针

    this->setMouseTracking(true);                 //鼠标移动捕捉
    this->canvasInit();
    this->initToolBar();
}

void Canvas::canvasInit()
{
    this->mPointS.rx()=0;
    this->mPointS.ry()=0;
    this->mPointE.rx()=0;
    this->mPointE.ry()=0;
    this->mPointDrag.rx()=0;
    this->mPointDrag.ry()=0;
    this->mDrawFlag=DrawStatus::waitDraw;

    this->mLineList.clear();
    this->mRectList.clear();
    this->mEllipseList.clear();

    this->mDrawPen.setBrush(Qt::red);
    this->mDrawPen.setWidthF(2.0);
    this->mDrawPen.setStyle(Qt::SolidLine);
}

void Canvas::initToolBar()
{
    this->mpToolbar=new QWidget(this);

    auto pMainToolBar=new QWidget();
    auto pMainToolLayout=new QHBoxLayout();
    this->mpBtn_cancel=new QPushButton(tr("Quit"));
    this->mpBtn_saveClipboard=new QPushButton(tr("Copy"));
    this->mpBtn_saveFile=new QPushButton(tr("Save"));
    this->mpBtn_drawLine=new QPushButton(tr("Line"));
    this->mpBtn_drawRect=new QPushButton(tr("Rect"));
    this->mpBtn_drawEllipse=new QPushButton(tr("Ellipse"));
    this->mpBtn_showOnDesktop=new QPushButton(tr("Show"));

    this->mpBtn_cancel->setStyleSheet(Const::UnSelect_btn_Color);
    this->mpBtn_saveClipboard->setStyleSheet(Const::UnSelect_btn_Color);
    this->mpBtn_saveFile->setStyleSheet(Const::UnSelect_btn_Color);
    this->mpBtn_showOnDesktop->setStyleSheet(Const::UnSelect_btn_Color);

    pMainToolLayout->addWidget(this->mpBtn_drawLine);
    pMainToolLayout->addWidget(this->mpBtn_drawRect);
    pMainToolLayout->addWidget(this->mpBtn_drawEllipse);
    pMainToolLayout->addWidget(this->mpBtn_cancel);
    pMainToolLayout->addWidget(this->mpBtn_saveClipboard);
    pMainToolLayout->addWidget(this->mpBtn_saveFile);
    pMainToolLayout->addWidget(this->mpBtn_showOnDesktop);
    pMainToolLayout->setContentsMargins(0,0,0,0);
    pMainToolLayout->setSpacing(0);
    pMainToolBar->setLayout(pMainToolLayout);

    this->mpShapeToolBar=new QWidget();
    auto pShapeToolLayout=new QHBoxLayout();

    this->mpCbx_lineSize=new QComboBox();
    this->mpBtn_colorSelect=new QPushButton();
    this->mpCbx_lineStyle=new QComboBox();

    QStringList sizeItems;
    for (int i=1; i <= 20; ++i ){
        sizeItems<<QString::number(i);
    }

    this->mpCbx_lineSize->addItems(sizeItems);
    this->mpBtn_colorSelect->setStyleSheet("background-color: rgb(255, 0, 0);");
    for (int i=1; i <= 5; ++i ){
        this->mpCbx_lineStyle->addItem(QIcon(QString(":/pic/%1.ico").arg(i)),"");
    }
    this->mpCbx_lineStyle->setIconSize(QSize(80,20));
    pShapeToolLayout->addWidget(this->mpCbx_lineSize);
    pShapeToolLayout->addWidget(this->mpBtn_colorSelect);
    pShapeToolLayout->addWidget(this->mpCbx_lineStyle);
    pShapeToolLayout->addStretch();
    pShapeToolLayout->setContentsMargins(0,0,0,0);
    pShapeToolLayout->setSpacing(0);
    this->mpShapeToolBar->setLayout(pShapeToolLayout);
    this->mpShapeToolBar->setVisible(false);

    this->mpCbx_lineSize->setCurrentText(QString::number(mDrawPen.width()));
    QColor color=mDrawPen.brush().color();
    QString colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(color.red()).arg(color.green()).arg(color.blue());
    this->mpBtn_colorSelect->setStyleSheet(colorStyle);
    switch (mDrawPen.style()) {
    case Qt::SolidLine:
        this->mpCbx_lineStyle->setCurrentIndex(0);
        break;
    case Qt::DashLine:
        this->mpCbx_lineStyle->setCurrentIndex(1);
        break;
    case Qt::DotLine:
        this->mpCbx_lineStyle->setCurrentIndex(2);
        break;
    case Qt::DashDotLine:
        this->mpCbx_lineStyle->setCurrentIndex(3);
        break;
    case Qt::DashDotDotLine:
        this->mpCbx_lineStyle->setCurrentIndex(4);
        break;
    default:
        this->mpCbx_lineStyle->setCurrentIndex(0);
        break;
    }

    auto pToolLayout=new QVBoxLayout();
    pToolLayout->addWidget(pMainToolBar);
    pToolLayout->addWidget(this->mpShapeToolBar);
    pToolLayout->setContentsMargins(0,0,0,0);
    pToolLayout->setSpacing(0);
    this->mpToolbar->setLayout(pToolLayout);
    this->mpToolbar->setVisible(false);

    this->mpBtn_drawLine->setStyleSheet(Const::UnSelect_btn_Color);
    this->mpBtn_drawRect->setStyleSheet(Const::UnSelect_btn_Color);
    this->mpBtn_drawEllipse->setStyleSheet(Const::UnSelect_btn_Color);

    this->hideToolBar();

    connect(this->mpBtn_drawLine, SIGNAL(clicked(bool)),this,SLOT(slt_drawLine()));
    connect(this->mpBtn_drawRect, SIGNAL(clicked(bool)),this,SLOT(slt_drawRect()));
    connect(this->mpBtn_drawEllipse, SIGNAL(clicked(bool)),this,SLOT(slt_drawEllipse()));
    connect(this->mpBtn_cancel, SIGNAL(clicked(bool)),this,SLOT(slt_cancel()));
    connect(this->mpBtn_saveClipboard, SIGNAL(clicked(bool)),this,SLOT(slt_saveClipboard()));
    connect(this->mpBtn_saveFile, SIGNAL(clicked(bool)),this,SLOT(slt_saveFile()));
    connect(this->mpBtn_showOnDesktop, SIGNAL(clicked(bool)),this,SLOT(slt_showOnDesktop()));

    connect(this->mpCbx_lineSize, SIGNAL(currentTextChanged(QString)),this,SLOT(slt_changePenWidth(QString)));
    connect(this->mpBtn_colorSelect, SIGNAL(clicked(bool)),this,SLOT(slt_changePenColor()));
    connect(this->mpCbx_lineStyle, SIGNAL(currentIndexChanged(int)),this,SLOT(slt_changePenStyle(int)));
}

void Canvas::showToolBar()
{
    qreal x,y;
    int bar_width=this->mpToolbar->width();
    int bar_height=this->mpToolbar->height();
    int offset=5;

    if(this->mShotArea.left()+bar_width+offset<this->mScreen_width){
        x=this->mShotArea.left()+offset;
    }else{
        x=this->mScreen_width-(bar_width+offset);
    }

    if(this->mShotArea.bottom()+bar_height+offset<this->mScreen_height){
        y=this->mShotArea.bottom()+offset;
    }else if(this->mShotArea.top()>bar_height+offset){
        y=this->mShotArea.top()-(bar_height+offset);
    }else if(this->mShotArea.top()<=bar_height+offset){
        y=offset;
    }else{
        y=this->mShotArea.top()+offset;
    }
    this->mpToolbar->move(x,y);
    this->mpToolbar->setVisible(true);
}

RectPaint Canvas::getRectF(QPointF p1, QPointF p2)
{
    float x1,y1,x2,y2;
    if(p1.x()<p2.x()){
        x1=p1.x();
        x2=p2.x();
    }else{
        x1=p2.x();
        x2=p1.x();
    }

    if(p1.y()<p2.y()){
        y1=p1.y();
        y2=p2.y();
    }else{
        y1=p2.y();
        y2=p1.y();
    }
    QPointF ps(x1,y1);
    QPointF pe(x2,y2);
    RectPaint tRect(ps,pe);
    return tRect;
}

void Canvas::hideToolBar()
{
    this->mpToolbar->setVisible(false);
}

void Canvas::refrashToolBar()
{
    this->mpBtn_drawLine->setText(tr("Line"));
    this->mpBtn_drawRect->setText(tr("Rect"));
    this->mpBtn_drawEllipse->setText(tr("Ellipse"));
    this->mpBtn_cancel->setText(tr("Quit"));
    this->mpBtn_saveClipboard->setText(tr("Copy"));
    this->mpBtn_saveFile->setText(tr("Save"));
    this->mpBtn_showOnDesktop->setText(tr("Show"));
}

void Canvas::changeLanguage(QString lan)
{
    QTranslator translator;
    bool bRes=false;
    if(Const::L_ZH_CN==lan){
        bRes=translator.load(Const::F_L_ZH_CN);
    }else if(Const::L_EN==lan){
        bRes=translator.load(Const::F_L_EN);
    }

    if(bRes){
        QApplication::installTranslator(&translator);
        refrashToolBar();
    }
}

quint8 Canvas::caputerRect(QRectF t_rect, qreal t_x, qreal t_y)
{
    quint8 pos=0;

    bool isLeft=qAbs(t_x-t_rect.topLeft().x())<2;
    bool isRight=qAbs(t_x-t_rect.bottomRight().x())<2;
    bool isBetweenLeftAndRight=(t_x>=t_rect.topLeft().x()+2)
            && (t_x<=t_rect.bottomRight().x()-2);
    bool isTop=qAbs(t_y-t_rect.topLeft().y())<2;
    bool isBottom=qAbs(t_y-t_rect.bottomRight().y())<2;
    bool isBetweenTopAndBottom=(t_y>=t_rect.topLeft().y()+2)
            && (t_y<=t_rect.bottomRight().y()-2);

    if(isTop){
        //上
        pos=1;
    }else if(isBottom){
        //下
        pos=2;
    }else if(isBetweenTopAndBottom){
        //上下之间
        pos=3;
    }else{
        pos=0;
    }

    if(isLeft){
        //左
        return pos;
    }else if(isRight){
        return 2*pos;
    }else if(isBetweenLeftAndRight) {
        return 3*pos;
    }else{
        return 0;
    }
}

void Canvas::shootScreen(QRectF &rect)
{
    auto pScreen = QGuiApplication::primaryScreen();
    this->mOriginalPixmap=pScreen->grabWindow(0,rect.x(),rect.y(),rect.width(),rect.height());
}

void Canvas::setbgPixmap(QPixmap &px)
{
    this->mFullPixmap=px;
}

void Canvas::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        if(DrawStatus::waitDraw==this->mDrawFlag){
            mPointS.rx()=event->x();
            mPointS.ry()=event->y();
            mPointE.rx()=event->x();
            mPointE.ry()=event->y();
            mShotArea=getRectF(this->mPointS,this->mPointE);
            this->mDrawFlag=DrawStatus::drawing;
        }else if(DrawStatus::drew==this->mDrawFlag){
            switch (this->mDrawEditFlag) {
            case 0:
            {
                mPointDrag.setX(event->x());
                mPointDrag.setY(event->y());
                break;
            }
            case 1://画直线
            {
                mPointS.rx()=event->x();
                mPointS.ry()=event->y();
                mPointE.rx()=event->x();
                mPointE.ry()=event->y();
                LinePaint tempLine(this->mPointS.toPoint(),this->mPointE.toPoint());
                tempLine.setPen(this->mDrawPen);
                this->mLineList.append(tempLine);
                break;
            }
            case 2://画矩形
            {
                mPointS.rx()=event->x();
                mPointS.ry()=event->y();
                mPointE.rx()=event->x();
                mPointE.ry()=event->y();
                RectPaint tempRect=getRectF(this->mPointS,this->mPointE);
                tempRect.setPen(this->mDrawPen);
                this->mRectList.append(tempRect);
                break;
            }
            case 3://椭圆
            {
                mPointS.rx()=event->x();
                mPointS.ry()=event->y();
                mPointE.rx()=event->x();
                mPointE.ry()=event->y();
                RectPaint tempRect=getRectF(this->mPointS,this->mPointE);
                tempRect.setPen(this->mDrawPen);
                this->mEllipseList.append(tempRect);
                break;
            }
            default:
            {
                break;
            }
            }//end-switch (this->mDrawEditFlag)
        }//end-if(DrawStatus::drew==this->mDrawFlag)
    }else if(event->button()==Qt::RightButton){
        this->setCursor(Qt::ArrowCursor);
        if(DrawStatus::waitDraw==this->mDrawFlag){
            this->slt_cancel();
        }else{
            this->canvasInit();
            this->hideToolBar();
            this->update();
        }
    }

}

void Canvas::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()==Qt::LeftButton){
        if(DrawStatus::drawing==this->mDrawFlag){
            this->mPointE.setX(event->x());
            this->mPointE.setY(event->y());
            this->mShotArea=this->getRectF(this->mPointS,this->mPointE);
        }else if(DrawStatus::drew==this->mDrawFlag){
            QPointF tempTL, tempBR;
            tempTL=this->mShotArea.topLeft();
            tempBR=this->mShotArea.bottomRight();
            switch (this->mCursorCaptureFlag) {
            case 0:
                this->setCursor(Qt::ArrowCursor);
                break;
            case 1:
                this->setCursor(Qt::SizeFDiagCursor);
                tempTL.setX(event->x());
                tempTL.setY(event->y());
                break;
            case 2:
                this->setCursor(Qt::SizeBDiagCursor);
                tempTL.setX(event->x());
                tempBR.setY(event->y());
                break;
            case 3:
                this->setCursor(Qt::SizeHorCursor);
                tempTL.setX(event->x());
                break;
            case 4:
                this->setCursor(Qt::SizeBDiagCursor);
                tempTL.setX(event->x());
                tempBR.setY(event->y());
                break;
            case 5:
                this->setCursor(Qt::SizeFDiagCursor);
                tempBR.setX(event->x());
                tempBR.setY(event->y());
                break;
            case 6:
                this->setCursor(Qt::SizeHorCursor);
                tempBR.setX(event->x());
                break;
            case 7:
                this->setCursor(Qt::SizeVerCursor);
                tempTL.setY(event->y());
                break;
            case 8:
                this->setCursor(Qt::SizeVerCursor);
                tempTL.setY(event->y());
                break;
            case 9:
                if(0==this->mDrawEditFlag){
                    this->setCursor(Qt::SizeAllCursor);
                    qreal dx=event->x()-this->mPointDrag.x();
                    qreal dy=event->y()-this->mPointDrag.y();

                    this->mPointDrag.setX(event->x());
                    this->mPointDrag.setY(event->y());

                    if((tempTL.x()+dx>0)&&(tempBR.x()+dx<this->mScreen_width)){
                        tempTL.rx()+=dx;
                        tempBR.rx()+=dx;
                    }
                    if((tempTL.y()+dy>0)&&(tempBR.y()+dy<this->mScreen_height)){
                        tempTL.ry()+=dy;
                        tempBR.ry()+=dy;
                    }
                }else if(1==this->mDrawEditFlag){
                    this->mPointE.setX(event->x());
                    this->mPointE.setY(event->y());

                    this->mLineList.last().setPoints(this->mPointS.toPoint(),this->mPointE.toPoint());
                }else if(2==this->mDrawEditFlag){
                    this->mPointE.setX(event->x());
                    this->mPointE.setY(event->y());
                    this->mRectList.last().setTopLeft(this->mPointS);
                    this->mRectList.last().setBottomRight(this->mPointE);
                }else if(3==this->mDrawEditFlag){
                    this->mPointE.setX(event->x());
                    this->mPointE.setY(event->y());
                    this->mEllipseList.last().setTopLeft(this->mPointS);
                    this->mEllipseList.last().setBottomRight(this->mPointE);
                }
                break;
            default:
                break;
            }//end-switch (this->mCursorCaptureFlag)
            this->mShotArea.setTopLeft(tempTL);
            this->mShotArea.setBottomRight(tempBR);
            this->showToolBar();
        }//end-if(DrawStatus::drew==this->mDrawFlag)
        update();
    }else if(event->buttons()==Qt::NoButton){
        if(DrawStatus::drew==this->mDrawFlag){
            this->mCursorCaptureFlag=this->caputerRect(this->mShotArea,event->x(),event->y());
            switch (this->mCursorCaptureFlag) {
            case 0:
                this->setCursor(Qt::ArrowCursor);
                break;
            case 1:
                this->setCursor(Qt::SizeFDiagCursor);
                break;
            case 2:
                this->setCursor(Qt::SizeBDiagCursor);
                break;
            case 3:
                this->setCursor(Qt::SizeHorCursor);
                break;
            case 4:
                this->setCursor(Qt::SizeBDiagCursor);
                break;
            case 5:
                this->setCursor(Qt::SizeFDiagCursor);
                break;
            case 6:
                this->setCursor(Qt::SizeHorCursor);
                break;
            case 7:
                this->setCursor(Qt::SizeVerCursor);
                break;
            case 8:
                this->setCursor(Qt::SizeVerCursor);
                break;
            case 9:
                if(0==this->mDrawEditFlag){
                    this->setCursor(Qt::SizeAllCursor);
                }else if(1==this->mDrawEditFlag){
                    this->setCursor(Qt::ArrowCursor);
                }
                break;
            default:
                break;
            }//end-switch (this->mCursorCaptureFlag)
        }//end-if(DrawStatus::drew==this->mDrawFlag)
    }//end-if(event->buttons()==Qt::NoButton)
}

void Canvas::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        if(DrawStatus::drawing==this->mDrawFlag){
            this->mPointE.setX(event->x());
            this->mPointE.setY(event->y());
            this->mShotArea=this->getRectF(this->mPointS,this->mPointE);
            this->mDrawFlag=DrawStatus::drew;
            this->showToolBar();
        }else if(DrawStatus::drew==this->mDrawFlag){
            QPointF distance=this->mPointE-this->mPointS;
            switch (this->mDrawEditFlag) {
            case 0:
                this->showToolBar();
                break;
            case 1:
                if(distance.manhattanLength() < 2){
                    this->mLineList.removeLast();
                }
                break;
            case 2:
                if(distance.manhattanLength() < 2){
                    this->mRectList.removeLast();
                }
                break;
            case 3:
                if(distance.manhattanLength() < 2){
                    this->mEllipseList.removeLast();
                }
                break;
            default:
                break;
            }
        }
        update();
    }
}

void Canvas::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);

    QPixmap tempMask(this->mScreen_width,this->mScreen_height);
    tempMask.fill(QColor(0,0,0,160));

    painter.drawPixmap(0,0,this->mFullPixmap);
    painter.drawPixmap(0,0,tempMask);

    switch (this->mDrawFlag) {
    case DrawStatus::waitDraw:
        break;
    case DrawStatus::drawing:
    case DrawStatus::drew:
        painter.setPen(QPen(Qt::green,2,Qt::DashLine));
        painter.drawRect(this->mShotArea);
        painter.drawPixmap(this->mShotArea,this->mFullPixmap,this->mShotArea);
        break;
    default:
        break;
    }

    for(quint16 i=0;i<this->mLineList.length();++i){
        painter.setPen(this->mLineList[i].getPen());
        painter.drawLine(this->mLineList[i]);
    }
    for(quint16 i=0;i<this->mRectList.length();++i){
        painter.setPen(this->mRectList[i].getPen());
        painter.drawRect(this->mRectList[i]);
    }
    for(quint16 i=0;i<this->mEllipseList.length();++i){
        painter.setPen(this->mEllipseList[i].getPen());
        painter.drawEllipse(this->mEllipseList[i]);
    }
    QWidget::paintEvent(e);
}

void Canvas::slt_drawLine()
{
    if(1!=this->mDrawEditFlag){
        this->mDrawEditFlag=1;
        this->mpBtn_drawLine->setStyleSheet(Const::Select_Btn_Color);
        this->mpBtn_drawRect->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawEllipse->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpShapeToolBar->setVisible(true);
    }else{
        this->mDrawEditFlag=0;
        this->mpBtn_drawLine->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawRect->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawEllipse->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpShapeToolBar->setVisible(false);
    }
    this->mpToolbar->adjustSize();
    this->showToolBar();
}

void Canvas::slt_drawRect()
{
    if(2!=this->mDrawEditFlag){
        this->mDrawEditFlag=2;
        this->mpBtn_drawLine->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawRect->setStyleSheet(Const::Select_Btn_Color);
        this->mpBtn_drawEllipse->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpShapeToolBar->setVisible(true);
    }else{
        this->mDrawEditFlag=0;
        this->mpBtn_drawLine->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawRect->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawEllipse->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpShapeToolBar->setVisible(false);
    }
    this->mpToolbar->adjustSize();
    this->showToolBar();
}

void Canvas::slt_drawEllipse()
{
    if(3!=this->mDrawEditFlag){
        this->mDrawEditFlag=3;
        this->mpBtn_drawLine->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawRect->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawEllipse->setStyleSheet(Const::Select_Btn_Color);
        this->mpShapeToolBar->setVisible(true);
    }else{
        this->mDrawEditFlag=0;
        this->mpBtn_drawLine->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawRect->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpBtn_drawEllipse->setStyleSheet(Const::UnSelect_btn_Color);
        this->mpShapeToolBar->setVisible(false);
    }
    this->mpToolbar->adjustSize();
    this->showToolBar();
}

void Canvas::slt_saveFile()
{
    this->shootScreen(this->mShotArea);
    QDateTime currentTime=QDateTime::currentDateTime();
    QString currentTimeStr=currentTime.toString("yyyyMMdd_hhmmss");
    QString fileName="ShotScreen_"+currentTimeStr+".png";
    QString path=OperateSet::readSetting("Setting","QuickSaveDir",".").toString();
    QString wholeFileName=QFileDialog::getSaveFileName(this,tr("Save File"),path+"/"+fileName,tr("PNG File (*.png)"));
    this->mOriginalPixmap.save(wholeFileName,"png");

    QFileInfo fi = QFileInfo(wholeFileName);
    QString newPath=fi.absolutePath();

    if(newPath!=path){
        OperateSet::writeSetting("Setting","QuickSaveDir",newPath);
    }
    this->slt_cancel();
}

void Canvas::slt_saveClipboard()
{
    this->shootScreen(this->mShotArea);
    this->mpClipboard->setPixmap(this->mOriginalPixmap);
    this->slt_cancel();
}

void Canvas::slt_showOnDesktop()
{
    this->shootScreen(this->mShotArea);
    emit sig_addWindow2Desktop(this->mOriginalPixmap);
    this->slt_cancel();
}

void Canvas::slt_cancel()
{
    this->mDrawFlag=DrawStatus::waitDraw;
    this->close();
}

void Canvas::slt_changePenColor()
{
    QColor color=QColorDialog::getColor();
    if(color.isValid()){
        QString colorStyle=QString("background-color: rgb(%1, %2, %3);").arg(color.red()).arg(color.green()).arg(color.blue());
        this->mpBtn_colorSelect->setStyleSheet(colorStyle);
    }
    this->mDrawPen.setColor(color);
}

void Canvas::slt_changePenWidth(QString s)
{
    this->mDrawPen.setWidth(s.toInt());
}

void Canvas::slt_changePenStyle(int index)
{
    Qt::PenStyle penStyle;
    switch (index) {
    case 0:
        penStyle=Qt::SolidLine;
        break;
    case 1:
        penStyle=Qt::DashLine;
        break;
    case 2:
        penStyle=Qt::DotLine;
        break;
    case 3:
        penStyle=Qt::DashDotLine;
        break;
    case 4:
        penStyle=Qt::DashDotDotLine;
        break;
    default:
        break;
    }
    this->mDrawPen.setStyle(penStyle);
}
